package com.eric.es;

import com.eric.es.document.LogEntity;
import com.eric.es.document.ProjectEntity;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.beans.BeansException;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.SearchHit;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.data.elasticsearch.core.query.Query;

import java.time.LocalDate;
import java.util.List;
import java.util.UUID;

/**
 * @ClassName BootstrapApplication
 * @Description TODO
 * @Author Eric
 * @Date 2021/12/31 11:00
 * @Version 1.0
 */
@Slf4j
@SpringBootApplication
public class BootstrapApplication implements ApplicationContextAware {
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {

        ElasticsearchRestTemplate esRestTemplate = (ElasticsearchRestTemplate)applicationContext.getBean("esRestTemplate");

        // 插入数据
//        insert(esRestTemplate,"新华字典", "插入数据");
//        insert(esRestTemplate,"增加新人", "插入数据");
//        insert(esRestTemplate,"黑客增长", "插入数据");
//        insert(esRestTemplate,"黑客", "插入数据");

//        query(esRestTemplate);

//        ----------------------------------

//        insertProject(esRestTemplate,0,"test project name");

        queryProject(esRestTemplate);

    }

    /**
     * 直接插入数据
     * @param esRestTemplate
     * @param title
     * @param content
     */
    private void insert(ElasticsearchRestTemplate esRestTemplate,String title,String content){
        String id = UUID.randomUUID().toString().replaceAll("-", "");
        LogEntity logEntity = new LogEntity(id, "info", title, content, LocalDate.now());
        esRestTemplate.save(logEntity);
    }

    private void insertProject(ElasticsearchRestTemplate esRestTemplate,long id,String projectName){
        ProjectEntity projectEntity = new ProjectEntity(id, projectName);
        esRestTemplate.save(projectEntity);
    }

    /**
     * 模糊的分页查询
     * 查询 title 列 ，模糊匹配
     * @param esRestTemplate
     */
    private void queryProject(ElasticsearchRestTemplate esRestTemplate){
        log.info(" queryProject >>>> ");
        // 查询数据
        PageRequest pageRequest = PageRequest.of(0, 10);

        NativeSearchQueryBuilder searchQueryBuilder = new NativeSearchQueryBuilder();
        searchQueryBuilder.withQuery(QueryBuilders.matchQuery("project_name", "工程"))
                .withPageable(pageRequest);

        Query searchQuery = searchQueryBuilder.build();
        SearchHits<ProjectEntity> hits = esRestTemplate.search(searchQuery, ProjectEntity.class);
        List<SearchHit<ProjectEntity>> hitList = hits.getSearchHits();
        log.info("hit size -> {}", hitList.size());
        hitList.forEach(hit -> {
            log.info("返回数据：{}", hit.getContent());
        });
    }

    /**
     * 模糊的分页查询
     * 查询 title 列 ，模糊匹配
     * @param esRestTemplate
     */
    private void query(ElasticsearchRestTemplate esRestTemplate){
        // 查询数据
        PageRequest pageRequest = PageRequest.of(0, 10);

        NativeSearchQueryBuilder searchQueryBuilder = new NativeSearchQueryBuilder();
        searchQueryBuilder.withQuery(QueryBuilders.matchQuery("title", "增"))
                .withPageable(pageRequest);

        Query searchQuery = searchQueryBuilder.build();
        SearchHits<LogEntity> hits = esRestTemplate.search(searchQuery, LogEntity.class);
        List<SearchHit<LogEntity>> hitList = hits.getSearchHits();
        log.info("hit size -> {}", hitList.size());
        hitList.forEach(hit -> {
            log.info("返回数据：{}", hit.getContent());
        });
    }

    public static void main(String[] args) {
        SpringApplication.run(BootstrapApplication.class,args);
    }
}
